clear all
set type double
set varabbrev on, perm

*** FILL IN MAIN PATH TO RUN CODE ***
global dropbox "..."

global folder "$dropbox/_0_bjps_replication"
global in "$folder/in"
global out "$folder/out"
global proc "$folder/proc"

global graph "$out/graph"
global table "$out/table"
global temp "`c(tmpdir)'"

************************
import excel using "$proc/explain_partisan_return.xlsx", firstrow clear case(lower)
keep survey_id explain raclassification *category* label
ren raclassification cat_1
ren ndcategory cat_2
ren rdcategory cat_3
sa "$temp/return", replace

***
u "$proc/survey_reg_base.dta", clear
g survey_id = cond(!mi(case_id), case_id, prolific_pid)
merge 1:1 survey_id using "$temp/return", keep(1 3)assert(1 3)nogen

g group = 1 if employer_SBO == 1
replace group = 2 if single_SBO == 1
replace group = 3 if small_biz == 0

bysort group label:egen tot = total(weight) //denominator

keep survey_id weight group cat_* tot label
reshape long cat_, i(survey_id weight group tot label)j(rank)
drop if mi(cat_)

collapse(rawsum)weight, by(group tot cat_ label)
g pct = weight/tot*100
sort label group cat_

replace cat_ = "Anything Else" if regexm(cat_, "Anything Else")
replace cat_ = "Other Economic Issues" if regexm(cat_, "Other Economic Issues")

g cat_rank = cat_ == "Regulatory Policy Views and Size of Government "
replace cat_rank = 2 if cat_ == "Other Economic Issues"
replace cat_rank = 3 if cat_ == "Social Issues"
replace cat_rank = 4 if cat_ == "Other Policy Issues"
replace cat_rank = 5 if cat_ == "Identity / Group Affiliations"
replace cat_rank = 6 if cat_ == "Anything Else"

drop tot
reshape wide weight pct, i(label cat_*)j(group)
sort label cat_rank

*** table ***
format pct* %12.1fc

	g tab = "\begin{tabular}{lrrr}" in 1
	g top = "\toprule" in 1
	*Midrule
	g mid = " \midrule" in 1 
	g hline = " \hline" in 1 
	*Bottomrule
	g bot = "\bottomrule" in 1
	*End
	g end = "\end{tabular}" in 1
	*Panel

	g title1 = " & Employer SBO & Non-Employer SBO & Non-SBO \\" in 1 
	g title2 = " & \% & \% & \% \\" in 1 
	
	g row0 = "\textit{Democrats}& & & \\" in 1
	g row1 = "\textit{Republicans}& & & \\" in 1

	
local filename = "5_partisan_cat"

	listtex tab if _n == 1 using "$table/`filename'.tex", replace rstyle(none)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex title1 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	
	listtex row0 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex cat_ pct1 pct2 pct3 if label == "Democrat", appendto("$table/`filename'.tex") rstyle(tabular)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	
	listtex row1 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex cat_ pct1 pct2 pct3 if label == "Republican", appendto("$table/`filename'.tex") rstyle(tabular)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)

	listtex bot if _n == 1, appendto("$table/`filename'.tex") rstyle(none)	
	listtex end if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
